﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kokomo.Environment;
using System.Reflection;

namespace Kokomo.Environment.Hosting
{
	/// <summary>
	/// Represents a document containing information about a component.
	/// </summary>
	/// <remarks>
	/// The environment provides a predefined view, but the application
	/// or editor may extend this class and provide an alternative view.
	/// The default implementation extracts the product and version information
	/// from assembly metadata.
	/// </remarks>
	[ViewType(typeof(AboutDocumentView))]
	public class AboutDocumentViewModel : DocumentViewModel
	{
		/// <summary>
		/// Initializes a new instance of <see cref="AboutDocumentViewModel"/>.
		/// </summary>
		/// <param name="assembly">The primary <see cref="Assembly"/> implementing the component.</param>
		/// <param name="productUrl">The URL of the product web site.</param>
		/// <exception cref="ArgumentNullException"><paramref name="assembly"/> is <c>null</c>.</exception>
		/// <remarks>
		/// The implementation extracts the product and version information from
		/// <paramref name="assembly"/>.  See <see cref="AboutDocumentViewModel"/>
		/// for details.
		/// </remarks>
		public AboutDocumentViewModel(
			Assembly assembly,
			string productUrl
			)
		{
			if (assembly == null) throw new ArgumentNullException("assembly");

			AssemblyProductAttribute productAttribute = assembly.GetCustomAttribute<AssemblyProductAttribute>();
			if (productAttribute != null)
				this.ProductName = productAttribute.Product;

			AssemblyCopyrightAttribute copyrightAttribute = assembly.GetCustomAttribute<AssemblyCopyrightAttribute>();
			if (copyrightAttribute != null)
				this.CopyrightText = copyrightAttribute.Copyright;

			AssemblyFileVersionAttribute versionAttribute = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
			if (versionAttribute != null)
				this.VersionText = versionAttribute.Version;

			this.ProductUrl = productUrl;
		}

		/// <summary>
		/// Gets the product name.
		/// </summary>
		/// <remarks>
		/// This value is extracted from the <see cref="AssemblyProductAttribute"/>.
		/// </remarks>
		public string ProductName { get; private set; }
		/// <summary>
		/// Gets the copyright text.
		/// </summary>
		/// <remarks>
		/// This value is extracted from the <see cref="AssemblyCopyrightAttribute"/>.
		/// </remarks>
		public string CopyrightText { get; private set; }
		/// <summary>
		/// Gets the version.
		/// </summary>
		/// <remarks>
		/// This value is extracted from the <see cref="AssemblyFileVersionAttribute"/>.
		/// </remarks>
		public string VersionText { get; private set; }

		/// <summary>
		/// Gets the URL of the product web page.
		/// </summary>
		public string ProductUrl { get; private set; }
	}
}
